package com.yxjx.duoxue;
import java.util.ArrayList;

import com.yxjx.duoxue.R;
import com.yxjx.duoxue.course.CourseIntroductionActivity;
import com.yxjx.duoxue.course.TeacherIntroductionActivity;
import com.yxjx.duoxue.datamodel.DMTeacher;
import com.yxjx.duoxue.network.NwUtils;
import com.yxjx.duoxue.utils.UIHelper;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;


public class YxTearchersHSVHelper {
	
	private Context mContext;
	private HorizontalScrollView mHsv;
	private LinearLayout mContainer;
	
	public YxTearchersHSVHelper(Context context) {
		mContext = context;
	}
	
	public View getView() {
		View ret;
		ret = LayoutInflater.from(mContext).inflate(R.layout.item_horizonal_scrollview, null);
		mHsv = (HorizontalScrollView) ret.findViewById(R.id.horizontalScrollView);
		mContainer = (LinearLayout) ret.findViewById(R.id.container);
		
		return ret;
	}
	
	public void setAdapter(BaseAdapter adapter) {
		for (int i = 0; i < adapter.getCount(); i++) {
			mContainer.addView(adapter.getView(i, null, null));
		}
	}
	
	public class HsvAdapter extends BaseAdapter {
		
		// 加载数据大 hzl teachers
		private ArrayList<DMTeacher> mTeachers = new ArrayList<DMTeacher>();
		
		public HsvAdapter(ArrayList<DMTeacher> teachers) {
			mTeachers = teachers;
		}
		
		public void setTeachers(ArrayList<DMTeacher> teachers) {
			mTeachers = teachers;
			notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			if (mTeachers == null) {
				return 0;
			}
			return mTeachers.size();
		}
		
		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			View ret;
			if (arg1 == null) {
				ret = LayoutInflater.from(mContext).inflate(R.layout.item_teacher_hzl, null);
			} else {
				ret = arg1;
			}
			// customize teacher hzl teacher item
			final DMTeacher teacher = mTeachers.get(arg0);
			
			UIHelper.setText(ret, R.id.teacher_name,
					teacher.getTeacherName());
			UIHelper.asyncLoadCircleImage(
					(ImageView) ret.findViewById(R.id.teacher_img),
					teacher.getIconUrl(),
					R.drawable.default_head_icon);
			
			ret.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					Intent intent = new Intent(arg0.getContext(), TeacherIntroductionActivity.class);
					intent.putExtra(NwUtils.KEY_ID, "" + teacher.getId());
					arg0.getContext().startActivity(intent);
				}
			});
			
			return ret;
		}

		@Override
		public Object getItem(int arg0) {
			return arg0;
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

	}
	
}
